package com.microservice.salmon.weixin.config;

import com.microservice.salmon.weixin.store.DefaultWeiXinTicketStore;
import com.microservice.salmon.weixin.store.WeiXinTicketStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 微信开发配置类
 * </p>
 *
 * @author 王洪庆 2018-12-11 12:02
 */
@Configuration
public class WeixinConfig {

    /**
     * 可以自行实现WeiXinAccessTokenStore进行配置，如果没有自定义的实现，将采用默认的配置
     * 如果是分布式环境， 请采用redis 或者 zookeeper等实现
     */
    @Bean
    @ConditionalOnMissingBean(WeiXinTicketStore.class)
    public WeiXinTicketStore weiXinTicketStore() {
        return new DefaultWeiXinTicketStore();
    }

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
}
